package com.yesep.learn.design.common.pojo;

import com.yesep.learn.design.common.*;

import java.io.*;
import java.util.*;

/**
 * 电脑
 */
public class Computer implements Serializable {
    private String model;
    private String audio;
    private String keyboard;
    private String master;
    private String mouse;
    private String screen;

    private List<IComputerComponent> components;

    public Computer() {
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getAudio() {
        return audio;
    }

    public void setAudio(String audio) {
        this.audio = audio;
    }

    public String getKeyboard() {
        return keyboard;
    }

    public void setKeyboard(String keyboard) {
        this.keyboard = keyboard;
    }

    public String getMaster() {
        return master;
    }

    public void setMaster(String master) {
        this.master = master;
    }

    public String getMouse() {
        return mouse;
    }

    public void setMouse(String mouse) {
        this.mouse = mouse;
    }

    public String getScreen() {
        return screen;
    }

    public void setScreen(String screen) {
        this.screen = screen;
    }

    public List<IComputerComponent> getComponents() {
        return components;
    }

    public void setComponents(List<IComputerComponent> components) {
        this.components = components;
    }

    public void addComponent(IComputerComponent component) {
        List<IComputerComponent> list = getComponents();
        if (list == null) {
            list = new ArrayList<>();
            setComponents(list);
        }
        list.add(component);
    }

    public void removeComponent(IComputerComponent component) {
        if (null == components) return;
        components.removeIf(e -> e.getModel().equals(component.getModel()));
    }

    public double calculateConsumption() {
        if (null == components) return 0;
        return components.stream().mapToDouble(IComputerComponent::getConsumption).sum();
    }

    @Override
    public String toString() {
        return String.format("Computer{model='%s', audio='%s', keyboard='%s', master='%s', mouse='%s', screen='%s'}",
                model, audio, keyboard, master, mouse, screen);
    }
}
